<template>
  <div class='container'>
    <el-card>
      <el-row
        type="flex"
        justify="space-between"
      >
        <el-col :span="10">
          <span style="margin-right:15px;font-weight:700">学科名称</span>
          <el-input
            style="width:200px;margin-right:15px"
            v-model="pageInfo.subjectName"
          ></el-input>
          <el-button @click="btnClear">清除</el-button>
          <el-button
            type="primary"
            @click="btnSearch"
          >搜索</el-button>
        </el-col>
        <el-col :span="2.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            @click="btnAdd"
          >新增学科</el-button>
        </el-col>
      </el-row>
      <!-- 警告信息模块 -->
      <el-row class="warn">
        <el-alert
          type="info"
          show-icon
        >
          <span slot="title">
            共有{{pageInfo.total}}条数据
          </span>
        </el-alert>
      </el-row>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="subjectName"
          label="学科名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="创建者"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="addDate"
          label="创建日期"
          align="center"
        >
          <template slot-scope="{row}">
            <span>{{row.addDate|formatDate}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="isFrontDisplay"
          label="前台是否显示"
          align="center"
          :formatter="formatterIsShow"
        >
        </el-table-column>
        <el-table-column
          prop="twoLevelDirectory"
          label="二级目录"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="tags"
          label="标签"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="totals"
          label="题目数量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop=""
          label="操作"
          align="center"
        >

          <template slot-scope="{row}">
            <el-button
              type="text"
              @click="$router.push(`/subjects/directorys/?id=${row.id}&name=${row.subjectName}`)"
            >学科分类</el-button>
            <el-button
              type="text"
              @click="$router.push(`/subjects/tags/?id=${row.id}&name=${row.subjectName}`)"
            >学科标签</el-button>
            <el-button
              type="text"
              @click="btnEdit(row.id)"
            >修改</el-button>
            <el-button
              type="text"
              @click="btnDel(row.id)"
            >删除</el-button>
          </template>

        </el-table-column>
      </el-table>
      <el-row
        type="flex"
        justify="end"
        style="height:60px;"
        align="middle"
      >
        <el-col
          :span="7"
          style="margin-top:20px"
        >
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="pageInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
          />

        </el-col>
      </el-row>

    </el-card>
    <el-dialog
      :visible.sync="showDialog"
      :title="showTitle"
      width="30%"
      @close="btnCancel"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
      >
        <el-form-item
          prop="subjectName"
          label="学科名称"
        >
          <el-input
            v-model="addForm.subjectName"
            style="width:150px"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="是否显示"
          style="margin-left:13px"
        >
          <el-switch
            v-model="addForm.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>

      </el-form>
      <span slot="footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button
          type="primary"
          @click="btnOK"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 引入接口函数
import { list, remove, update, add, detail, simple } from '@/api/hmmm/subjects'
// 引入时间格式过滤器
import { formatDate } from '@/filters'

export default {
  data () {
    return {

      tableData: [], //表格数据

      pageInfo: { //分页信息
        page: 1,
        pagesize: 5,
        total: 0,
        subjectName: ''
      },

      addForm: { //表单数据对象
        subjectName: '',
        isFrontDisplay: 1

      },

      rules: { //表单校验规则

        subjectName: [
          {
            required: true,
            message: '学科名称不能为空',
            trigger: 'blur'
          }
        ],
      },

      showDialog: false //控制新增/编辑弹层显示
    }
  },

  created () { //初始化列表
    this.getSubjectsList()
  },

  methods: {

    async getSubjectsList () { //学科列表
      const { data } = await list(this.pageInfo) //请求
      this.tableData = data.items
      this.pageInfo.total = data.counts
    },

    handleSizeChange (pagesize) { //pagesize变化时触发该函数
      this.pageInfo.pagesize = pagesize
      this.getSubjectsList()
    },

    handleCurrentChange (currentPage) { //当前页码值变化时触发该函数
      this.pageInfo.page = currentPage
      this.getSubjectsList() //刷新列表
    },

    formatterIsShow (row, column, cellValue, index) { //格式化'前台是否显示'数据
      return cellValue === 1 ? '是' : '否'
    },

    btnClear () { //清除搜索框按钮
      this.pageInfo.subjectName = ''
      this.getSubjectsList() //刷新列表
    },

    btnSearch () { //搜索按钮
      this.getSubjectsList(this.pageInfo)
    },

    async btnDel (id) { //删除学科
      await remove({ id: id })
      this.$message.success('删除成功')
      this.getSubjectsList() //刷新列表
    },

    btnAdd () { //添加学科
      this.showDialog = true //打开弹层
    },

    async btnEdit (id) { //修改学科
      const { data } = await detail({ id: id }) //获取学科详情
      this.addForm = data
      console.log(data);
      this.showDialog = true
    },

    btnCancel () { //取消按钮
      this.addForm = { //清空表单
        subjectName: '',
        isFrontDisplay: 1
      }
      this.$refs.addForm.resetFields() //移除校验
      this.showDialog = false //关闭弹层
    },

    async btnOK () { //确定按钮
      await this.$refs.addForm.validate()
      if (this.addForm.id) {
        await update(this.addForm)
      } else {
        await add(this.addForm)
      }
      this.$message.success('操作成功')
      this.getSubjectsList() //刷新列表
      this.showDialog = false //关闭弹层
    }
  },

  computed: {
    showTitle () { //根据学科id动态显示弹层title
      return this.addForm.id ? '修改学科' : '添加学科'
    }
  }
}
</script>

<style scoped lang='scss'>
.warn {
  margin: 20px 0;
}
</style>
